package com.itheima.reggie.controller;

import com.itheima.reggie.ResultInfo;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //发送短信
    @PostMapping("/user/sendMsg")
    public ResultInfo sendSms(@RequestBody Map<String, String> map) {
        //1.接受参数
        String phone = map.get("phone");
        //2.调用service
        userService.sendSms(phone);
        return ResultInfo.success("短信发送成功！");
    }


    //登录注册
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        //获取参数
        String phone = map.get("phone");
        String code = map.get("code");

        //调用service
        ResultInfo resultInfo = userService.login(phone, code);
        //返回结果
        return resultInfo;
    }


    //退出
    @PostMapping("/user/logout")
    public ResultInfo logout(@RequestHeader("Authorization") String token) {
        //获取token
        token = token.replace("Bearer", "");
        //调用service 进行删除
        userService.delete(token);
        return ResultInfo.success(null);
    }
}
